## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
import os
import sys

import Options
import Logs
import Utils
import Task

# Required NSC version
NSC_RELEASE_NAME = "nsc-0.5.1"


def set_options(opt):
    opt.add_option('--with-nsc',
                   help=('Use Network Simulation Cradle, given by the indicated path,'
                         ' to allow the use of real-world network stacks'),
                   default='', dest='with_nsc')


def configure(conf):
    conf.env['ENABLE_NSC'] = False

    # checks for flex and bison, which is needed to build NSCs globaliser
    # TODO: how to move these checks into the allinone scripts?
    #def check_nsc_buildutils():
    #    import flex
    #    import bison
    #    conf.check_tool('flex bison')
    #    conf.check(lib='fl', mandatory=True)

    # Check for the location of NSC
    if Options.options.with_nsc:
        if os.path.isdir(Options.options.with_nsc):
            conf.check_message("NSC location", '', True, ("%s (given)" % Options.options.with_nsc))
            conf.env['WITH_NSC'] = os.path.abspath(Options.options.with_nsc)
    else:
        nsc_dir = os.path.join('..', "nsc")
        if os.path.isdir(nsc_dir):
            conf.check_message("NSC location", '', True, ("%s (guessed)" % nsc_dir))
            conf.env['WITH_NSC'] = os.path.abspath(nsc_dir)
        del nsc_dir
    if not conf.env['WITH_NSC']:
        conf.check_message("NSC location", '', False)
        conf.report_optional_feature("nsc", "Network Simulation Cradle", False,
                                     "NSC not found (see option --with-nsc)")
	return
    
    if sys.platform in ['linux2']:
        arch = os.uname()[4]
    else:
        arch = None
    ok = False
    if arch == 'x86_64' or arch == 'i686' or arch == 'i586' or arch == 'i486' or arch == 'i386':
        conf.env['NSC_ENABLED'] = 'yes'
        conf.env.append_value('CXXDEFINES', 'NETWORK_SIMULATION_CRADLE')
        conf.check(mandatory=True, lib='dl', define_name='HAVE_DL', uselib='DL')
        ok = True
    conf.check_message('NSC supported architecture', arch, ok)
    conf.report_optional_feature("nsc", "Network Simulation Cradle", ok,
                                 "architecture %r not supported" % arch)

    # append the NSC kernel dirs to the module path so that these dirs
    # will end up in the LD_LIBRARY_PATH, thus allowing the NSC NS-3
    # module to find the necessary NSC shared libraries.
    for nsc_module in ['linux-2.6.18', 'linux-2.6.26']:
        conf.env.append_value('NS3_MODULE_PATH',
                              os.path.abspath(os.path.join(conf.env['WITH_NSC'], nsc_module)))



def build(bld):
    obj = bld.create_ns3_module('internet-stack', ['node'])
    obj.source = [
        'tcp-test.cc',
        'udp-test.cc',
        'ipv4-test.cc',
        'ipv4-l4-protocol.cc',
        'udp-header.cc',
        'tcp-header.cc',
        'ipv4-interface.cc',
        'ipv4-l3-protocol.cc',
        'ipv4-end-point.cc',
        'udp-l4-protocol.cc',
        'tcp-l4-protocol.cc',
        'arp-header.cc',
        'arp-cache.cc',
        'arp-l3-protocol.cc',
        'udp-socket-impl.cc',
        'tcp-socket-impl.cc',
        'ipv4-end-point-demux.cc',
        'udp-socket-factory-impl.cc',
        'tcp-socket-factory-impl.cc',
        'pending-data.cc',
        'sequence-number.cc',
        'rtt-estimator.cc',
        'ipv4-raw-socket-factory-impl.cc',
        'ipv4-raw-socket-impl.cc',
        'icmpv4.cc',
        'icmpv4-l4-protocol.cc',
        'loopback-net-device.cc',
        'ndisc-cache.cc',
        'ipv6-interface.cc',
        'icmpv6-header.cc',
        'ipv6-l3-protocol.cc',
        'ipv6-end-point.cc',
        'ipv6-end-point-demux.cc',
        'ipv6-l4-protocol.cc',
        'ipv6-raw-socket-factory-impl.cc',
        'ipv6-raw-socket-impl.cc',
        'ipv6-autoconfigured-prefix.cc',
        'ipv6-extension.cc',
        'ipv6-extension-header.cc',
        'ipv6-extension-demux.cc',
        'ipv6-option.cc',
        'ipv6-option-header.cc',
        'ipv6-option-demux.cc',
        'icmpv6-l4-protocol.cc',
        'ipv6-test.cc',
        'ipv6-extension-header-test-suite.cc',
        ]

    headers = bld.new_task_gen('ns3header')
    headers.module = 'internet-stack'
    headers.source = [
        'udp-header.h',
        'tcp-header.h',
        'sequence-number.h',
        'icmpv4.h',
        'icmpv6-header.h',
        # used by routing
        'ipv4-interface.h',
        'ipv4-l3-protocol.h',
        'ipv6-l3-protocol.h',
        'ipv6-extension-header.h',
        'ipv6-option-header.h',
        'arp-l3-protocol.h',
        'udp-l4-protocol.h',
        'tcp-l4-protocol.h',
        'icmpv4-l4-protocol.h',
        'ipv4-l4-protocol.h',
        'arp-cache.h',
        'icmpv6-l4-protocol.h',
        'ipv6-l4-protocol.h',
        'ndisc-cache.h',
	'loopback-net-device.h'
       ]

    if bld.env['NSC_ENABLED']:
        obj.source.append ('nsc-tcp-socket-impl.cc')
        obj.source.append ('nsc-tcp-l4-protocol.cc')
        obj.source.append ('nsc-tcp-socket-factory-impl.cc')
        obj.source.append ('nsc-sysctl.cc')
        headers.source.append('nsc-tcp-l4-protocol.h')
        obj.uselib = 'DL'
